package com.mahjong.server.manager;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.mahjong.server.common.EmRoomStatus;
import com.mahjong.server.model.Room;

/**
 * 定时任务
 * 
 * @author Simple
 * @date 2013-4-17 下午10:48:40
 * @Description TODO
 */
@Component
public class QuartzTask {

  private Logger log=Logger.getLogger(this.getClass());

  @Autowired
  private RoomManager roomManager;

  @Autowired
  private HeartBeatManager heartBeatManager;

  /**
   * 每隔30分钟对空闲房间进行清理
   */
  // @Scheduled(cron = "0 0/30 * * * ?")
  public void cleanFreeRoom() {
    List<Room> rooms=roomManager.getAllRoom();
    Iterator<Room> it=rooms.iterator();
    while(it.hasNext()) {
      Room room=it.next();
      if(room.getStatus() == EmRoomStatus.WAITING && room.getPlayerCount() == 0) {
        it.remove();
        log.debug("定时清理房间任务:清除" + room);
      }
    }
    log.debug(roomManager.getAllRoom().size());
  }

  @Scheduled(cron="0/30 * * * * ?")
  public void heartbeat() {
    // log.debug("心跳检测");
    heartBeatManager.run();
  }
}
